package com.github.ilioili.justdoit.components.receiver;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.NotificationCompat;

import com.github.ilioili.justdoit.components.activity.NotifyActivity;
import com.github.ilioili.justdoit.R;
import com.github.ilioili.justdoit.model.dao.ScheduleEventDao;
import com.github.ilioili.justdoit.model.pojo.ScheduleEvent;
import com.github.ilioili.justdoit.util.DateUtil;
import com.github.ilioili.justdoit.util.sql.DbUtil;

import java.util.List;

/**
 * Created by hantuo on 16/5/23.
 */
public class AlarmReceiver extends BroadcastReceiver {

    public static final String INTENT_KEY_ID = "id";
    public static final String JUST_DO_IT_ALARM_ACTION = "JustDoItAlarmAction";

    private void showNotification(ScheduleEvent scheduleEvent, Context context) {
//        RemoteViews remoteViews = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.notification);
//        remoteViews.setTextViewText(R.id.tvCC, "Hello");
        Intent it = NotifyActivity.newIntent(context, scheduleEvent);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) (scheduleEvent.id % 10000), it, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);
//        builder.setContent(remoteViews);
        builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND);
        builder.setSmallIcon(R.mipmap.icon).setContentTitle("Just do it !").setContentText(scheduleEvent.name);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify((int) (scheduleEvent.id % 10000), builder.build());
    }

    private static SharedPreferences sharedPreferences;


    public static void startAlarm(Context context, ScheduleEvent scheduleEvent, long time) {
        Intent intent = new Intent(JUST_DO_IT_ALARM_ACTION);
        intent.putExtra(INTENT_KEY_ID, scheduleEvent.id);
        PendingIntent sender = PendingIntent.getBroadcast(context, (int) (scheduleEvent.id % 10000), intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, time, sender);
    }


    public static void startAlarm(Context context, ScheduleEvent scheduleEvent) {
        if (scheduleEvent.isComplete) return;
        long time = DateUtil.getAlarmTime(scheduleEvent);
        if (time < System.currentTimeMillis()) {
            if (!scheduleEvent.isRepeat) {
                return;
            } else {
                time += DateUtil.DAY_MILISECOND;
            }
        }

        Intent intent = new Intent(JUST_DO_IT_ALARM_ACTION);
        intent.putExtra(INTENT_KEY_ID, scheduleEvent.id);
        PendingIntent sender = PendingIntent.getBroadcast(context, (int) (scheduleEvent.id % 10000), intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (scheduleEvent.isRepeat) {
            am.setRepeating(AlarmManager.RTC_WAKEUP, time, DateUtil.DAY_MILISECOND, sender);
        } else {
            am.set(AlarmManager.RTC_WAKEUP, time, sender);
        }
    }

    public static void cancelAlarm(Context context, ScheduleEvent scheduleEvent) {
        Intent intent = new Intent(JUST_DO_IT_ALARM_ACTION);
        intent.putExtra(INTENT_KEY_ID, scheduleEvent.id);
        PendingIntent sender = PendingIntent.getBroadcast(context, (int) (scheduleEvent.id % 10000), intent, 0);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.cancel(sender);
    }


    public static void setAlarm(Context context, ScheduleEvent scheduleEvent) {
        if (scheduleEvent.alarm) {
            startAlarm(context, scheduleEvent);
        } else {
            cancelAlarm(context, scheduleEvent);
        }
    }


    @Override
    public void onReceive(final Context context, Intent intent) {
        if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    DbUtil.init(context);
                    List<ScheduleEvent> list = ScheduleEventDao.getNeedAlarmEvent();
                    for (ScheduleEvent scheduleEvent : list) {
                        setAlarm(context, scheduleEvent);
                    }
                }
            }).start();
        } else {
            long id = intent.getLongExtra(INTENT_KEY_ID, 0);
            ScheduleEvent scheduleEvent = ScheduleEventDao.getById(id);
            if (null != scheduleEvent) {
                if (!scheduleEvent.isComplete && scheduleEvent.alarm) {
                    showNotification(scheduleEvent, context);
                }
            }
        }
    }
}
